<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webgl着色器</title>
  <script src="lib/webgl-debug.js"></script>
  <script src="lib/webgl-utils.js"></script>
  <script src="lib/cuon-utils.js"></script>
  <script src="lib/cuon-matrix.js"></script>
</head>

<body onload="main()">
  <canvas id="canvas" width="800" height="800"></canvas>
  <p id="near" style="text-align: center">这里将显示near和far的值</p>

</body>
<script>
  // 顶点着色器伪代码
  var vShder_source = `
  attribute vec4 position;
  attribute vec4 color;
  uniform mat4 mat;
  varying vec4 col;
void main(){
gl_Position = mat*position;
col = color;
}
`
  //片元着色器伪代码
  var fShder_source = `
  precision mediump float ;
  varying vec4 col;
void main(){
gl_FragColor = col; //设置颜色
}
`

  var near;

  var ex=0.25,ey =0.25,ez =0.25;
  var gnear =0;
  var gfar =1;
  function main() {
    var canvas = document.getElementById('canvas');
    near = document.getElementById('near');
    //  获取上下文对象
    var gl = getWebGLContext(canvas);
    //  初始化着色器
    if (!initShaders(gl, vShder_source, fShder_source)) {
      return;
    }

    var n = initVertexBuffers(gl);
    var mat = gl.getUniformLocation(gl.program, "mat");
    // 指定一个覆盖canvas的颜色
    gl.clearColor(0, 0, 0, 1.0);
    draw(gl, n, mat)
    document.onkeydown = function (event) {
      keydown(event, gl, n, mat);
    }

    // // 清除指定的缓冲区
    // gl.clear(gl.COLOR_BUFFER_BIT);
    // gl.drawArrays(gl.POINTS, 0, 1);
  }

  function keydown(event, gl, n, mat) {
    var code = event.keyCode;
    switch (code) {
      case 37: gfar -= 0.01; break;//左
      case 38: gnear += 0.01; break;//上
      case 39: gfar += 0.01; break;//右
      case 40: gnear -= 0.01; break;//下
      default: return;//其它
    }
    draw(gl, n, mat);
  }

  function draw(gl, n, mat) {
    near.innerText = `near:${gnear}, far:${gfar}`;
    var viewMatrix = new Matrix4();
    viewMatrix.setLookAt(ex, ey, ez, 0, 0, 0, 0, 1, 0);

    var modelMatrix = new Matrix4();
    modelMatrix.setRotate(-10, 0, 0, 1);

    var orthMarix = new Matrix4();
    orthMarix.setOrtho(-1, 1, -1, 1, gnear, gfar);

    var modeViewMatrix = orthMarix.multiply(viewMatrix.multiply(modelMatrix));

    gl.uniformMatrix4fv(mat, false, modeViewMatrix.elements);
    gl.clear(gl.COLOR_BUFFER_BIT);

    gl.drawArrays(gl.TRIANGLES, 0, n);


  }

  //初始化缓冲区
  function initVertexBuffers(gl) {
    var vertexDatas = new Float32Array([

      //顶点的坐标和颜色
      0.0, 0.5, -0.4, 0.4, 1.0, 0.4, // 绿色三角形，在最后面的三个点
      -0.5, -0.5, -0.4, 0.4, 1.0, 0.4,
      0.5, -0.5, -0.4, 1.0, 0.4, 0.4,

      0.5, 0.4, -0.2, 1.0, 0.4, 0.4, // 黄色三角形，在中间的三个点
      -0.5, 0.4, -0.2, 1.0, 1.0, 0.4,
      0.0, -0.6, -0.2, 1.0, 1.0, 0.4,

      0.0, 0.5, 0.0, 0.4, 0.4, 1.0,  // 蓝色三角形，在最前面的三个点
      -0.5, -0.5, 0.0, 0.4, 0.4, 1.0,
      0.5, -0.5, 0.0, 1.0, 0.4, 0.4

    ]);
    var n = 9;

    var vertexbuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertexDatas, gl.STATIC_DRAW);
    var size = vertexDatas.BYTES_PER_ELEMENT;
    var position = gl.getAttribLocation(gl.program, "position");
    var color = gl.getAttribLocation(gl.program, "color");
    gl.vertexAttribPointer(position, 3, gl.FLOAT, false, size * 6, 0);
    gl.enableVertexAttribArray(position);

    gl.vertexAttribPointer(color, 3, gl.FLOAT, false, size * 6, size * 3);
    gl.enableVertexAttribArray(color);




    return n;


  }

</script>

</html>